﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using prototype123.models;
using prototype123.controllers;

namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //user table in the database.
    class UserDAO
    {
        private SqlConnection db_connection;// = prototype123.Program.connect.GetConnection();

        public UserDAO(SqlConnection connection)
        {
            db_connection = connection;
        }

        /*precondition: system needs to validate login information provided by user
        description: Given string name and string password, Load checks the db to see if the
              username and password match. if they match, it returns a User object
              If they do not match, it returns null
        postcondition: the system now knows whether or not login succeeded.*/
        public User Load(string name, string pass)
        {
            User usr = new User();
            SqlCommand password;
            string passwordResult;

            //try
            //{
            password = new SqlCommand("SELECT EDU_ID, PASSWORD FROM USERS WHERE USERNAME = '" + name + "'", db_connection);
            SqlDataReader reader = password.ExecuteReader();
            reader.Read();
            int IDResult = Convert.ToInt32(reader[0].ToString());
            passwordResult = reader[1].ToString();
            reader.Close();
            //}
            //catch { return null; }

            usr.Username = name;
            usr.Password = passwordResult;
            usr.EducatorID = IDResult;
            return usr;
        }
    }
}
